// 把服务端打包出来的  server-bundle  渲染到html 中
const fs = require('fs');
const path = require('path');
let express = require('express')
let app = express()
let Vue = require('vue')
let VueServerRenderer = require('vue-server-renderer')

let template = fs.readFileSync(path.join(__dirname, './dist/index.ssr.html'), 'utf8')

// 改用json方式
// let serverBundle = fs.readFileSync('./dist/server.bundle.js', 'utf8')
let serverBundle = require('./dist/vue-ssr-server-bundle.json')
let clientManifest = require('./dist/vue-ssr-client-manifest.json')

let render = VueServerRenderer.createBundleRenderer(serverBundle,{
  template,
  clientManifest   // 
})



app.get('/', (req, res)=>{
  console.log('来了')
  let context = {url: req.url}  // 上下文  当前访问的路径往下传  让路由去匹配
  // 这个方法把context往下传 执行server.bundle.js 传递这个context
  render.renderToString(context, (err, html)=>{

    res.end(html)
  })
})

app.use(express.static(path.resolve(__dirname, 'dist')))

app.get('*', (req, res)=>{
  let context = {url: req.url}  // 上下文  当前访问的路径往下传  让路由去匹配
  // 这个方法把context往下传 执行server.bundle.js 传递这个context
  render.renderToString(context, (err, html)=>{
    res.end(html)
  })
})

app.listen(3000)